package linkedlist;

import java.util.Iterator;

public class DoubleLinkedListSentinel implements Iterable<Integer> {
    @Override
    public Iterator<Integer> iterator() {
        return new Iterator<Integer>() {
            Node pointer = head.next;

            @Override
            public boolean hasNext() {
                return pointer != tail;
            }

            @Override
            public Integer next() {
                int value = pointer.value;
                pointer = pointer.next;
                return value;
            }
        };
    }

    private static class Node {
        private Node prev;
        private Node next;
        private int value;

        public Node(Node prev, Node next, int value) {
            this.prev = prev;
            this.next = next;
            this.value = value;
        }
    }

    private Node head;
    private Node tail;

    public DoubleLinkedListSentinel() {
        head = new Node(null, null, 666);
        tail = new Node(null, null, 888);
        head.next = tail;
        tail.prev = head;
    }

    /**
     * 索引查取node节点
     *
     * @param index 目标索引
     * @return 返回节点
     */
    private Node findNode(int index) {
        int i = -1;
        for (Node pointer = head; pointer != tail; pointer = pointer.next, i++) {
            if (i == index) {
                return pointer;
            }
        }
        return null;
    }

    /**
     * 抛出违法索引异常
     *
     * @param index 违法索引
     * @return 索引异常
     */
    private IllegalArgumentException illegalIndex(int index) {
        return (new IllegalArgumentException(String.format("您输入的索引[%d]不合法！", index)));
    }

    /**
     * 通过索引插入节点
     *
     * @param index 目标索引
     * @param value 目标值
     */
    public void insert(int index, int value) {
        Node prev = findNode(index - 1);
        if (prev == null) {
            throw illegalIndex(index);
        }
        Node next = prev.next;
        Node inserted = new Node(prev, next, value);
        prev.next = inserted;
        next.prev = inserted;
    }

    /**
     * 通过索引删除元素
     *
     * @param index 目标索引
     */
    public void remove(int index) {
        Node prev = findNode(index - 1);
        if (prev == null) { // 删除节点前节点不存在
            throw illegalIndex(index);
        }
        Node removed = prev.next;
        if (removed == tail) { // 删除节点为尾指针
            throw illegalIndex(index);
        }
        Node next = removed.next;
        prev.next = next;
        next.prev = prev;
    }

    public void addFist(int value) {
        insert(0, value);
    }

    public void removeFirst() {
        remove(0);
    }

    /**
     * 向最后一个索引插入节点
     *
     * @param value 节点值
     */
    public void addLast(int value) {
        Node last = tail.prev;
        Node added = new Node(last, tail, value);
        last.next = added;
        tail.prev = added;
    }

    /**
     * 删除最后一个节点
     */
    public void removeLast() {
        Node last = tail.prev;
        if(last == head){
            throw illegalIndex(0);
        }
        Node prev = last.prev;
        prev.next = tail;
        tail.prev = prev;
    }

    /**
     * 通过索引获取节点值
     *
     * @param index 目标索引
     * @return 返回节点值
     */
    public int get(int index) {
        Node node = findNode(index);
        if (node == null) {
            throw illegalIndex(index);
        }
        return node.value;
    }

}
